package com.bct.jxc.view.user;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.bct.jxc.common.CommonFactory;
import com.bct.jxc.common.StringUtil;
import com.bct.jxc.model.Item;
import com.bct.jxc.model.User;
import com.bct.jxc.server.UserServer;
import com.bct.jxc.view.LoginFrame;

import javax.swing.JTextField;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.ImageIcon;

/**
 * 操作员编辑
 * 
 * @author FPF
 *
 */
public class UserEditPanel extends JPanel implements ActionListener{
	private JTextField usernameTf;
	JComboBox comboBox_1;
	JComboBox comboBox;
	private UserServer userServer;
	Item selectedItem;

	/**
	 * Create the panel.
	 */
	public UserEditPanel() {

		JPanel panel = new JPanel();

		JPanel panel_1 = new JPanel();

		JPanel panel_2 = new JPanel();

		JPanel panel_3 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
						.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
						.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
						.addComponent(panel_3, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
					.addGap(29)
					.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(83, Short.MAX_VALUE))
		);

		JButton btnNewButton = new JButton("修改");
		btnNewButton.setIcon(new ImageIcon(UserEditPanel.class.getResource("/com/bct/jxc/images/change.png")));
		btnNewButton.setFont(new Font("楷体", Font.BOLD, 16));
		btnNewButton.addActionListener(this);
		panel_3.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("删除");
		btnNewButton_1.setIcon(new ImageIcon(UserEditPanel.class.getResource("/com/bct/jxc/images/delete.png")));
		btnNewButton_1.setFont(new Font("楷体", Font.BOLD, 16));
		btnNewButton_1.addActionListener(this);
		panel_3.add(btnNewButton_1);

		JLabel lblNewLabel_2 = new JLabel("操作员权限：");
		lblNewLabel_2.setFont(new Font("楷体", Font.BOLD, 16));
		panel_2.add(lblNewLabel_2);

		comboBox_1 = new JComboBox();
		comboBox_1.setMaximumRowCount(32);
		comboBox_1.setFont(new Font("宋体", Font.PLAIN, 14));
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] { "超级管理员","普通管理员","采购员","仓库员","销售员" }));

		panel_2.add(comboBox_1);

		JLabel lblNewLabel_1 = new JLabel("操作员姓名：");
		lblNewLabel_1.setFont(new Font("楷体", Font.BOLD, 16));
		panel_1.add(lblNewLabel_1);

		usernameTf = new JTextField();
		usernameTf.setFont(new Font("宋体", Font.PLAIN, 14));
		panel_1.add(usernameTf);
		usernameTf.setColumns(20);

		JLabel lblNewLabel = new JLabel("选择操作员：");
		lblNewLabel.setFont(new Font("楷体", Font.BOLD, 16));
		panel.add(lblNewLabel);

		comboBox = new JComboBox();
		comboBox.setMaximumRowCount(32);
		comboBox.setFont(new Font("宋体", Font.PLAIN, 14));
		// 给comboBox设置选中事件
		comboBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				selectAction();
			}

		});
		panel.add(comboBox);
		setLayout(groupLayout);
		userServer = CommonFactory.getUserServer();
		initComboBox();
//		if (LoginFrame.getLoginUser() != null) {
//			operTf.setText(LoginFrame.getLoginUser().getLoginName());
//		}
	}

	/**
	 * 初始化操作者下拉选择框
	 */
	public void initComboBox() {
		// 获取数据库中的数据
		List<User> list = userServer.finds(null);
		// 设置下来列表框中数据模型
		Vector<Item> items = new Vector<>();
		// 清空下拉列表框中的内容，免的重复
		comboBox.removeAllItems();
		// 迭代处理数据
		for (int i = 0; i < list.size(); i++) {
			Item item = new Item();
			item.setId(list.get(i).getLoginName());
			item.setName(list.get(i).getLoginName());
			if (items.contains(item)) {// 如果集合中包含该元素，就不在添加
				continue;
			}
			items.add(item);
			comboBox.addItem(item);
		}
	}

	/**
	 * 下拉列表框点击事件
	 */
	private void selectAction() {

		if (!(comboBox.getSelectedItem() instanceof Item)) {
			return;
		}
		selectedItem = (Item) comboBox.getSelectedItem();
		// 根据登录名查询数据库
		User o = userServer.getUser(selectedItem.getId());
		usernameTf.setText(o.getLoginName());
		switch (o.getPower()) {
		case "超级管理员":
            comboBox_1.setSelectedIndex(0);
			break;
		case "普通管理员":
			comboBox_1.setSelectedIndex(1);
			break;
		case "采购员":
			comboBox_1.setSelectedIndex(2);
			break;
		case "仓库员":
			comboBox_1.setSelectedIndex(3);
			break;
		case "销售员":
			comboBox_1.setSelectedIndex(4);
			break;
		default:
			break;
		}

	}
    /**
     * 点击事件
     */
	@Override
	public void actionPerformed(ActionEvent e) {
		String target = e.getActionCommand();
		switch (target) {
		case "修改":
			System.out.println(usernameTf.getText().trim());
			if(usernameTf.getText().trim().equals(LoginFrame.getLoginUser().getLoginName())) {
				JOptionPane.showMessageDialog(null, "当前用户正在操作不能修改！","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(StringUtil.isEmpty(usernameTf.getText().trim())) {
				JOptionPane.showMessageDialog(null, "真实姓名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			User o = new User();
			o.setLoginName(((Item)(comboBox.getSelectedItem())).getId());
			o.setUsername(usernameTf.getText().trim());
			o.setPower(comboBox_1.getSelectedItem().toString());
			boolean isFlag = userServer.update(o);
			if(isFlag==true) {
				JOptionPane.showMessageDialog(null, "操作员修改成功！","提示",JOptionPane.INFORMATION_MESSAGE);
				initComboBox();
			}else {
				JOptionPane.showMessageDialog(null, "操作员修改失败！","警告",JOptionPane.WARNING_MESSAGE);
			}
			break;
		case "删除":
			if(usernameTf.getText().trim().equals(LoginFrame.getLoginUser().getLoginName())) {
				JOptionPane.showMessageDialog(null, "当前用户正在操作不能修改！","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			int result = JOptionPane.showConfirmDialog(null, "是否要删除所选的操作员！","警告",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
			if(result==0) {
				String loginName =((Item)(comboBox.getSelectedItem())).getId();
				if(loginName.equals(LoginFrame.getLoginUser().getLoginName())) {
					JOptionPane.showMessageDialog(null, "不能删除当前登录的用户！","提示",JOptionPane.INFORMATION_MESSAGE);		
					return;
				}
				boolean flag = userServer.delete(loginName);
				if(flag==true) {
					JOptionPane.showMessageDialog(null, "操作员删除成功！","提示",JOptionPane.INFORMATION_MESSAGE);	
					initComboBox();
				}else {
					JOptionPane.showMessageDialog(null, "操作员删除失败！","警告",JOptionPane.WARNING_MESSAGE);
				}
			}
			break;
		default:
			break;
		}
		
	}

}
